package com.basejace.system.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.basejace.common.model.PubDictdata;
import com.basejace.core.base.BaseController;
import com.basejace.core.model.PageBootstrapTable;
import com.jfinal.json.FastJsonFactory;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class DictdataController extends BaseController {

	public DictdataController(){
		setModelClass(PubDictdata.class);
	}
	public void beforeShow() throws Exception {
		setAttr("groupcode", getPara("groupcode"));
	}
	public void beforeAdd() throws Exception {
		this.getModelEntity().set("groupcode", getPara("groupcode"));
		setAttr(this.getModelSimpleName(), this.getModelEntity());
	}
	public void list() {
		String groupcode = getPara("groupcode");
		String sqlExceptSelect = "from pub_dictdata where 1=1 ";
		List sqlCondition = new ArrayList();
		if (StrKit.notBlank(groupcode)) {
			sqlExceptSelect += "and groupcode = ? ";
			sqlCondition.add(groupcode);
		}

		sqlExceptSelect += " order by priority desc";
		int offset = getParaToInt("offset");
		int limit = getParaToInt("limit");
		int pageNumber = (offset) / limit + 1;
		
		Page<Record> page = Db.paginate(pageNumber, limit, "select * ", sqlExceptSelect , sqlCondition.toArray());
		
		List<Record> list = page.getList();
		List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Record object = (Record) iterator.next();
			rows.add(object.getColumns());
		}
		
		PageBootstrapTable p = new PageBootstrapTable();
		p.setTotal(page.getTotalRow());
		p.setPageSize(page.getPageSize());
		p.setPageNumber(page.getPageNumber());
		p.setRows(rows);
		log.info(FastJsonFactory.me().getJson().toJson(p).toString());
		
		renderJson(p);
		
	}
	
	
	
}
